//
//  BaziLifeViewController.m
//  心水宝典
//
//  Created by zr on 2018/8/28.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "BaziLifeViewController.h"
#import "BaziLifeInfoViewController.h"

@interface BaziLifeViewController ()

@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, weak)UIView * topView;
@property (nonatomic, weak)UILabel * topLabel;
@property (nonatomic, weak)UIImageView * centerImage;
@property (nonatomic, weak)UIImageView * bottomImageView;
@property (nonatomic, weak)UIButton * sureButton;

@end

@implementation BaziLifeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    
    [self setupNav];
    [self setupUI];
}

- (void)setupNav{
    
    self.title = @"八字算命";
    
    UIButton * backButton = [[UIButton alloc] init];
    
//    backButton.frame = CGRectMake(0, 0, 20 * SCREENH6, 20 * SCREENH6);
    backButton.size = CGSizeMake(20 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}


- (void)setupUI{
    
    UIImageView * backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    
    backImageView.image = ImageNamed(@"bj");
    backImageView.userInteractionEnabled = YES;
    
    self.backImageView = backImageView;
    
    [self.view addSubview:self.backImageView];
    
    
    UIView * topView = [UIView new];
    
    topView.backgroundColor = BLACK_COLOR;
    topView.alpha = 0.8;
    
    self.topView = topView;
    
    [self.view addSubview:self.topView];
    
    
    UILabel * topLabel = [UILabel new];
    
    topLabel.text = @"八字算命\n生辰八字，简称八字，是指一个人出生时的干支历日期；年月日时共四柱干支，每柱两字，合共八个字。生辰八字在中国民俗信仰中占有重要地位，古代中国道家、星相家据此推算人的命运的好坏。";
    topLabel.textColor = WHITE_COLOR;
    topLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    topLabel.numberOfLines = 0;
    topLabel.textAlignment = NSTextAlignmentCenter;
    
    self.topLabel = topLabel;
    
    [self.view addSubview:self.topLabel];
    
    
    UIImageView * centerImage = [[UIImageView alloc] init];
    
    centerImage.image = ImageNamed(@"bagua");
    
    self.centerImage = centerImage;
    
    [self.view addSubview:self.centerImage];
    
    
    UIImageView * bottomImageView = [[UIImageView alloc] init];
    
    bottomImageView.image = ImageNamed(@"long");
    
    self.bottomImageView = bottomImageView;
    
    [self.view addSubview:self.bottomImageView];
    
    
    UIButton *sureButton = [UIButton new];
    
    [sureButton setBackgroundColor:ColorWithRGB(0xD33D31)];
    [sureButton setTitle:@"立即测试" forState:UIControlStateNormal];
    [sureButton setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(clickSureButton) forControlEvents:UIControlEventTouchUpInside];
    sureButton.titleLabel.font = [UIFont systemFontOfSize:24 * SCREENH6];
    sureButton.layer.cornerRadius = 5 * SCREENH6;
    sureButton.layer.masksToBounds = YES;
    
    self.sureButton = sureButton;
    
    [self.view addSubview:self.sureButton];
}


- (void)viewDidLayoutSubviews{
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(13 * SCREENH6);
        make.height.mas_equalTo(120 * SCREENH6);
        make.width.mas_equalTo(285 * SCREENH6);
    }];
    
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.topView).offset(12 *SCREENH6);
        make.leading.mas_equalTo(self.topView).offset(12 * SCREENH6);
        make.trailing.mas_equalTo(self.topView).offset(-12 * SCREENH6);
    }];
    
    [self.centerImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.topView.mas_bottom).offset(35 * SCREENH6);
        make.width.height.mas_equalTo(234 * SCREENH6);
    }];
    
    [self.bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.centerImage.mas_bottom).offset(5 * SCREENH6);
        make.width.mas_equalTo(152 * SCREENH6);
        make.height.mas_equalTo(82 * SCREENH6);
    }];
    
    [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.bottomImageView.mas_bottom).offset(30 * SCREENH6);
        make.width.mas_equalTo(175 * SCREENH6);
        make.height.mas_equalTo(50 * SCREENH6);
    }];
}



- (void)clickSureButton{
    
    
    [self rotate360DegreeWithImageView];
}

-(void)rotate360DegreeWithImageView {
    
    CABasicAnimation * rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; //让其在z轴旋转
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];//旋转角度
    rotationAnimation.duration = 0.5; //旋转周期
    rotationAnimation.cumulative = YES;//旋转累加角度
    rotationAnimation.repeatCount = 10;//旋转次数
    [self.centerImage.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
//    [[SuanNetWorkTool sharedTools] POST:@"/index/Liuhedivine/horoscope" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//
//        [self.centerImage.layer removeAllAnimations];
//
//        NSDictionary * data = responseObject[@"data"];
//
//        NSString * character = data[@"character"];
//        NSString * education = data[@"education"];
//        NSString * kinsman = data[@"kinsman"];
//        NSString * health = data[@"health"];
//        NSString * marriage = data[@"marriage"];
//        NSString * cause = data[@"cause"];
//
//        HIDDEN_TAB;
//
//        BaziLifeInfoViewController * infoVC = [BaziLifeInfoViewController new];
//
//        infoVC.character = character;
//        infoVC.education = education;
//        infoVC.kinsman = kinsman;
//        infoVC.health = health;
//        infoVC.marriage = marriage;
//        infoVC.cause = cause;
//
//        [self.navigationController pushViewController:infoVC animated:YES];
//
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//
//    }];
    
//    [NSThread sleepForTimeInterval:3];
    
    int64_t delayInSeconds = 3.0; // 延迟的时间
    
    /*
     *  delta:
     *
     *  @parameter 1.时间参照，从此刻开始计时
     *  @parameter 2.延时多久，此处为秒级，还有纳秒等: 10ull * NSEC_PER_MSEC
     */
    
    __weak typeof(self)weakSelf = self;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [weakSelf delayMethod];
    });
}


- (void)delayMethod{
    
    HIDDEN_TAB;
    
    BaziLifeInfoViewController * infoVC = [BaziLifeInfoViewController new];
    
    [self.navigationController pushViewController:infoVC animated:YES];
}

-(void)stopRotate {
    
    [self.centerImage.layer removeAllAnimations];
}


- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end
